package com.jiantong.web.controller;

import com.jiantong.lawEnforcing.model.dbentity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by cao on 2017/9/8.
 */
@Controller
public class FormController {

    @RequestMapping(value="/temp/index",method =RequestMethod.GET )
    private String loadform(Model m)
    {
        //从数据库中查到文书的html 和它的其它相关信息
        String tmstr="<table>\n" +
                "\n" +
                "    <tr>\n" +
                "        <td><input name=\"danwei\" id=\"danwei\" value=\"{$danwei}\" /> </td>\n" +
                "    </tr>\n" +
                "\n" +
                "    <tr><td>\n" +
                "        当事人：<input name=\"person\" id=\"person\" value=\"{$person}\" />\n" +
                "    </td></tr>\n" +
                "\n" +
                "    <tr>\n" +
                "        <td>罚款：<input name=\"fakuan\" id=\"fakuan\" value=\"{$fakuan}\" /></td>\n" +
                "    </tr>\n" +
                "\n" +
                "    <tr><td>\n" +
                "        案件描述：<input name=\"anjianmiaoshu\" id=\"anjianmiaoshu\" value=\"{$anjianmiaoshu}\" />\n" +
                "\n" +
                "    </td></tr>\n" +
                "\n" +
                "</table>";

        //根据相关信息在数据库中拿到当前页面每个input的name和value
        List<dbentity> list = new ArrayList<>();

        dbentity db1 = new   dbentity();
        db1.setKeyname("danwei");
        db1.setKeyvalue("养殖场");
        list.add(db1);

         db1 = new   dbentity();
        db1.setKeyname("person");
        db1.setKeyvalue("李小明");
        list.add(db1);

        db1 = new   dbentity();
        db1.setKeyname("fakuan");
        db1.setKeyvalue("300");
        list.add(db1);

        db1 = new   dbentity();
        db1.setKeyname("anjianmiaoshu");
        db1.setKeyvalue("偷排污水");
        list.add(db1);


        for(dbentity db :list)
        {

            tmstr=  tmstr.replace("{$"+db.getKeyname()+"}",db.getKeyvalue());
        }



    m.addAttribute("tmstr",tmstr);
        return "temp/form";
    }

    @RequestMapping(value="/temp/saveform",method =RequestMethod.POST )
    private String saveform(HttpServletRequest request, HttpServletResponse response)
    {
       Map<String,String[]> mplist =  request.getParameterMap();
        dbentity db;

        List<dbentity> list = new ArrayList<>();

        for (String keyname : mplist.keySet()) {
                     //map.keySet()返回的是所有key的值
                    String[] keyvalue = mplist.get(keyname);//得到每个key多对用value的
          db= new dbentity();

          db.setKeyname(keyname);
          db.setKeyvalue(keyvalue.toString());
          list.add(db);

        }
        //把list里的各个db存储到数据库(更新到数据库)

        return "";
    }
}
